﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace CMSService
{
    /// <summary>
    /// 用户实现类
    /// </summary>
    public class UserService : ICMS.IUser
    {
        DavidDB.ADO.DBManager db = null;
        private UserService()
        {
            db = new DavidDB.ADO.DBManager();
        }
        public UserService(DavidDB.ADO.DBManager db)
        {
            this.db = db;
        }






        /// <summary>
        /// 获取一个网站用户
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns>网站用户，如果没有，返回Null</returns>
        public CMSModel.User GetModel(int id)
        {
            CMSModel.User returnValue = new CMSModel.User();
            returnValue.Id = id;
            returnValue = db.GetModel(returnValue);
            return returnValue;
        }
        /// <summary>
        /// 删除一个网站用户
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns>成功删除，返回0，没有当前用户，返回-1</returns>
        public int Delete(int id)
        {
            int returnValue = (int)CMSModel.DBOperatorStatus.Success;
            CMSModel.User model = new CMSModel.User();
            model.Id = id;
            db.Delete(model);
            return returnValue;
        }
        /// <summary>
        /// 增加一个网站用户
        /// </summary>
        /// <param name="announcement">网站用户实体</param>
        /// <returns>增加成功，返回0</returns>
        public int Insert(CMSModel.User user)
        {
            int returnValue = (int)CMSModel.DBOperatorStatus.Success;
            db.Insert(user);
            return returnValue;
        }
        /// <summary>
        /// 编辑一个用户
        /// </summary>
        /// <param name="announcement">编辑完成的用户</param>
        /// <returns>编辑成功，返回0</returns>
        public int Edit(CMSModel.User user)
        {
            int returnValue = (int)CMSModel.DBOperatorStatus.Success;
            db.Edit(user);
            return returnValue;
        }
        /// <summary>
        /// 到底最新的count条记录
        /// </summary>
        /// <param name="count">查询条数</param>
        /// <returns>如果记录足够多，返回count条，否则，返回所有记录</returns>
        public List<CMSModel.User> GetList(int count)
        {
            List<CMSModel.User> lists = new List<CMSModel.User>();
            var query = db.GetTopList(count, new CMSModel.User());
            foreach (CMSModel.User model in query)
            {
                lists.Add(model);
            }
            return lists;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>用户编号</returns>
        public int Login(string username, string password)
        {
            int userId = -1;
            CMSModel.User user = new CMSModel.User();
            user.Password = password;
            user.Username = username;
            List<dynamic> query = db.GetSomeObject(user);
            if (query.Count() > 0)
            {
                query.First().LoginCount += 1;
                userId = query.First().Id;
            }
            return userId;
        }
    }
}
